package com.matt.repository;

import com.matt.controller.api.domin.Thermometer;
import com.matt.domain.ChildrenHealthBaseInfo;
import com.matt.domain.ChildrenLeaveRequest;
import com.matt.domain.ChildrenMemorandum;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;


/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/18
 * *******************************
 */
public interface ChildrenLeaveRequestRepository extends JpaRepository<ChildrenLeaveRequest, String>, JpaSpecificationExecutor<ChildrenLeaveRequest> {



    /**
     * 查询宝宝的【已确认/已取消】请假记录中，与新请假时间重叠的记录
     * 状态限制：state in ('1', '2')，重叠条件：时间区间有交集
     */
    // 方法1：新增请假用（无排除自身条件）
    @Query("SELECT clr FROM ChildrenLeaveRequest clr " +
            "WHERE clr.chldrenId = :chldrenId " +
            "AND clr.state IN ('1', '2') " +
            "AND ((:newStart BETWEEN clr.startTime AND clr.endTime) " +
            "OR (:newEnd BETWEEN clr.startTime AND clr.endTime) " +
            "OR (clr.startTime BETWEEN :newStart AND :newEnd))")
    List<ChildrenLeaveRequest> findOverlappingLeaveByChldrenId(
            @Param("chldrenId") String chldrenId,
            @Param("newStart") Date newStart,
            @Param("newEnd") Date newEnd);

    // 方法2：编辑请假用（排除自身，修复参数问题）
    @Query("SELECT clr FROM ChildrenLeaveRequest clr " +
            "WHERE clr.chldrenId = :chldrenId " +
            "AND clr.id != :currentLeaveId " + // JPQL参数名改为和方法参数一致：currentLeaveId
            "AND clr.state IN ('1', '2') " +
            "AND ((:newStart BETWEEN clr.startTime AND clr.endTime) " +
            "OR (:newEnd BETWEEN clr.startTime AND clr.endTime) " +
            "OR (clr.startTime BETWEEN :newStart AND :newEnd))")
    List<ChildrenLeaveRequest> findOverlappingLeaveByChldrenId1(
            @Param("chldrenId") String chldrenId, // 每个参数都必须加@Param注解
            @Param("newStart") Date newStart,
            @Param("newEnd") Date newEnd,
            @Param("currentLeaveId") String currentLeaveId); // 注解名和JPQL参数名一致
}
